package com.hub.netty.base.domain;

import lombok.Getter;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;


@Getter
public enum MessageEnum {

    CONNECT(1, "建立连接"),

    CALL(2, "呼叫"),

    HANGUP(3, "挂机");

    public final Integer type;

    public final String content;

    MessageEnum(Integer type, String content) {
        this.type = type;
        this.content = content;
    }

    public static MessageEnum getStructureEnum(Message msg) {
        Integer type = Optional.ofNullable(msg)
                .map(Message::getMsgType)
                .orElse(0);
        if (type != 0) {
            List<MessageEnum> objectEnums = Arrays.stream(MessageEnum.values())
                    .filter((item) -> Objects.equals(item.getType(), type))
                    .distinct()
                    .collect(Collectors.toList());
            if (!objectEnums.isEmpty()) {
                return objectEnums.get(0);
            }
        }
        return null;
    }

}
